package com.ymatou.iapi.idcardquery.testcase;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.idcardquery.parameter.GetManageBuyerIdCardInfosReq;
import com.ymatou.iapi.idcardquery.resp.GetManageBuyerIdCardInfosResp;
import com.ymatou.iapi.idcardquery.resp.IdCardInfo;
import com.ymatou.iapi.idcardquery.resp.IdCardInfoVerify;
import com.ymatou.iapi.idcardquery.resp.RejectReasonEnum;
import com.ymatou.iapi.idcardquery.service.GetManageBuyerIdCardInfosCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
/**
*根据买家id从本地库查询所有收件人身份证信息列表接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetManageBuyerIdCardInfos {
	private static GetManageBuyerIdCardInfosReq getmanagebuyeridcardinfoBean;
	private static GetManageBuyerIdCardInfosCall getmanagebuyeridcardinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据买家id从本地库查询所有收件人身份证信息列表接口");
	}

	@Before
	public void caseUp() {
		getmanagebuyeridcardinfoBean = new GetManageBuyerIdCardInfosReq();
		getmanagebuyeridcardinfoCall = new GetManageBuyerIdCardInfosCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmanagebuyeridcardinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDb() {

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

		long buyerId = getmanagebuyeridcardinfoBean.getBuyerId();

		List<YmtIdCardInfo> expected = ymtIdCard.selectByBuyerIdList(buyerId);

		GetManageBuyerIdCardInfosResp actualResp = getmanagebuyeridcardinfoCall
				.getManageBuyerIdCardInfosResp();

		if (CollectionUtils.isEmpty(expected)) {

			Logger.verifyEquals(null, actualResp.getBuyerIdCardInfos(),
					"校验接口返回数据为null");

			return;
		}

		if (expected.size() != actualResp.getBuyerIdCardCount()) {

			Logger.verifyEquals(expected.size(),
					actualResp.getBuyerIdCardCount(), "校验接口返回数据记录");
			return;
		}

		List<IdCardInfo> actual = actualResp.getBuyerIdCardInfos();

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		for (int i = 0; i < expected.size(); i++) {

			YmtIdCardInfo expectedIdCardInfo = expected.get(i);
			IdCardInfo actualIdCardInfo = actual.get(i);

			Logger.comment("校验name:" + expectedIdCardInfo.getName());

			Logger.verifyEquals(expectedIdCardInfo.getCardId(),
					actualIdCardInfo.getCardId(), "校验CardId");

			Logger.verifyEquals(expectedIdCardInfo.getName(),
					actualIdCardInfo.getName(), "校验name");
			Logger.verifyEquals(expectedIdCardInfo.getCardNumber(),
					actualIdCardInfo.getCardNumber(), "校验CardNumber");

			Logger.verifyEquals(expectedIdCardInfo.getCardType(),
					actualIdCardInfo.getCardType(), "校验CardType");

			Logger.verifyEquals(expectedIdCardInfo.getStatus(),
					actualIdCardInfo.getStatus(), "校验Status");

			Logger.verifyEquals(expectedIdCardInfo.getMobile(),
					actualIdCardInfo.getMobile(), "校验Mobile");

			String rejectReason = null;

			int rejectReasonCode = expectedIdCardInfo.getRejectReason();

			if (expectedIdCardInfo.getStatus() == -1) {

				RejectReasonEnum rejectReasonEnum = RejectReasonEnum

				.getEnum(rejectReasonCode);

				rejectReason = "审核未通过";

				if (null != rejectReasonEnum) {
					rejectReason = rejectReasonEnum.getMessage();
				}
			}

			Logger.verifyEquals(rejectReason,
					actualIdCardInfo.getRejectReason(), "校验RejectReason");

			String expectedPicRightSide = "";

			String expectedPicReverseSide = "";

			//boolean isHasPic = true;

			if (!expectedIdCardInfo.getPicRightSide().isEmpty()) {

				String picUrl = IdCardInfoVerify
						.convertPicUrl(expectedIdCardInfo.getPicRightSide());

				expectedPicRightSide = IdCardInfoVerify.downloadPic(picUrl);

			} /*else {
				isHasPic = false;

				}*/

			if (!expectedIdCardInfo.getPicReverseSide().isEmpty()) {

				String picUrl = IdCardInfoVerify
						.convertPicUrl(expectedIdCardInfo.getPicReverseSide());
				expectedPicReverseSide = IdCardInfoVerify.downloadPic(picUrl);
			} /*else {
				isHasPic = false;
				}*/

			Logger.verifyEquals(expectedIdCardInfo.getHasPic(),
					actualIdCardInfo.getHasPic(), "校验HasPic");

			DataUtil.verifyEquals(expectedPicRightSide,
					actualIdCardInfo.getPicRightSide(), "校验PicRightSide");
			DataUtil.verifyEquals(expectedPicReverseSide,
					actualIdCardInfo.getPicReverseSide(), "校验PicReverseSide");

			Logger.verifyEquals(expectedIdCardInfo.getCheckIdCardStatus(),
					actualIdCardInfo.getCheckIdCardStatus(),
					"校验CheckIdCardStatus");

			DataUtil.verifyEquals(expectedIdCardInfo.getUploadTime(),
					actualIdCardInfo.getUploadTime(), "验证UploadTime");

		}

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfos_001() {
		Logger.start(true, "根据买家id查询身份证信息列表成功");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			getmanagebuyeridcardinfoBean.setBuyerId(buyerId);
			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfos_002() {
		Logger.start(true, "买家Id不存在");
		try {

			getmanagebuyeridcardinfoBean.setBuyerId(288483l);
			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfos_003() {
		Logger.start(true, "买家Id为null");
		try {

			//getmanagebuyeridcardinfoBean.setBuyerId(288483l);
			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("400",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			Logger.verifyEquals("100",
					getmanagebuyeridcardinfoCall.getString("bcode"), "验证bcode");

			Logger.verifyEquals("buyerId:买家Id未提供",
					getmanagebuyeridcardinfoCall.getString("msg"), "验证msg");

			//verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}